package cn.xi.service.checkItem;

import cn.xi.checkItem.MemberService;
import cn.xi.dao.MemberDao;
import cn.xi.pojo.Member;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.*;


@Service(interfaceClass = MemberService.class)
@Transactional
public class MemberServiceImpl implements MemberService {

    @Autowired
    private MemberDao memberDao;

    @Override
    public Member selectPhoneNumber(String telephone) {
        Member member = memberDao.selectPhoneNumber(telephone);
        return member;
    }

    @Override
    public void save(Member member) {
        memberDao.savephone(member);
    }

    @Override
    public List<Integer> selectTimeNumber(List<String> list) {
        List<Integer> list1 = new ArrayList<>();
        for (String s : list) {
            Integer integer = memberDao.selectTimeNumber(s + ".31");
            list1.add(integer);
        }
        return list1;
    }

    @Override
    public Map selectTimeNumber2() {
        Map map = new HashMap();
        Calendar calendar = Calendar.getInstance();
        Date time = calendar.getTime();
        calendar.add(Calendar.MONTH, -12);
        Date time1 = calendar.getTime();
        List<String> list = new ArrayList<>();
        for (int i = 0; i < 12; i++) {
            calendar.add(Calendar.MONTH, 1);
            list.add(new SimpleDateFormat("yyyy.MM").format(calendar.getTime()));
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("YYYY-MM-dd");
        SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("YYYY.MM");
        String format = simpleDateFormat.format(time);
        String format1 = simpleDateFormat.format(time1);
        List<Map> list1 = memberDao.selectTimeNumber2(format, format1);
         List<Long> integers =new ArrayList<>();
         List<String> strings =new ArrayList<>();
        for (String s : list) {
            for (int i = 0; i < list1.size(); i++) {
                Map map1 = list1.get(i);
                String regTime = simpleDateFormat2.format(map1.get("regTime"));
                if (s.equals(regTime)){
                   strings.add(s);
                    Long number = (Long) map1.get("number");
                    integers.add(number);
                    break;
                }
                if (i==(list1.size()-1)){
                    strings.add(s);
                    integers.add(0l);
                }
            }
        }
       map.put("months", strings);
       map.put("memberCount",integers);
        return map;
    }
}
